/*
	Prepare Follow-up 2 Data for appending
*/

********************************************************************************
*  LOAD DATA AND KEEP VARIABLES
********************************************************************************

use "$data/1_raw/followup2_raw-deidentified.dta", clear

keep matricul sexe groupe s1qs1 ///
    s1q4n s1q5 s1q3n s1q3nb s1aq5a s1aq5a_a ///
    s1bq6 s1bq6a s1bq6a_a s1aq13 s1bq13 s1aq14 s1bq14 ///
    s1bq15_m s1bq15a s1bq15 ///
    s1aq15 s1aq15a s1aq15b ///
    s1bq15d s1bq15e s1bq15e_a ///
    s3q3_1 s3q3_2 s3q3_3 s3q3_4 ///
    s3q3_5 s3q3_6 s3q3_7 s3q3_8 ///
    s3q4a s3q4b s3q4c s3q4d s3q4e ///
    s3q4f s3q4g s3q4h s3q4i s3q4k ///
    s4q1 s4q1_1 s4q1_2 s4q1_3 s4q1_4 s4q1_5 ///
    s4q3 s4q4 s4q4a s4q4b s4q4c ///
    s4q4d s4q4e s4q4f s4q4g s4q4h ///
    s5q1 s5q2 s5q3 s5q5 s5q6 ///
    s5q7 s5q8 s5q9 s1bq5a s1bq5a_a

********************************************************************************
*  VARIABLE RENAMING
********************************************************************************

rename matricul id
rename sexe sex
rename groupe treat
rename s1qs1 consent

rename s1q4n bef_eif_job
rename s1q5 eif_still_work

rename s1q3n resid_since
rename s1q3nb resid_prev_regi

rename s1aq5a eif_pos

rename s1bq6 last7d_status
rename s1bq6a currwork_sector

replace s1aq13 = s1bq13 if eif_still_work == 2
replace s1aq14 = s1bq14 if eif_still_work == 2
drop s1bq14 s1bq13

rename s1aq13 last7d_days_worked
rename s1aq14 last7d_hours_worked
rename s1bq15_m earn_avg_month
rename s1bq15a earn_avg_month_range
rename s1bq15 earn_last_month
rename s1aq15 earn_change_jan20
rename s1aq15a earn_dir_change_jan20
rename s1aq15b earn_amount_change_jan20
rename s1bq15d last7d_earn_avg
rename s1bq15e last7d_earn_freq
rename s1bq5a eif_quit_reason

rename s3q3_1 covid_protect_m1
rename s3q3_2 covid_protect_m2
rename s3q3_3 covid_protect_m3
rename s3q3_4 covid_protect_m4
rename s3q3_5 covid_protect_m5
rename s3q3_6 covid_protect_m6
rename s3q3_7 covid_protect_m7
rename s3q3_8 covid_protect_m8

rename s3q4a covid_sympt_cough
rename s3q4b covid_sympt_diff_breath
rename s3q4c covid_sympt_sore_throat
rename s3q4d covid_sympt_running_nose
rename s3q4e covid_sympt_muscle_pain
rename s3q4f covid_sympt_headache
rename s3q4g covid_sympt_tiredness
rename s3q4h covid_sympt_diarrhea
rename s3q4i covid_sympt_loss_smell
rename s3q4k covid_sympt_fever

rename s4q1 covid_change_work_cond
rename s4q1_1 covid_change_work_cond1
rename s4q1_2 covid_change_work_cond2
rename s4q1_3 covid_change_work_cond3
rename s4q1_4 covid_change_work_cond4
rename s4q1_5 covid_change_work_cond5

rename s4q3 covid_hhm_lost_job
rename s4q4 covid_feb_revenue
rename s4q4a covid_feb_tot_revenue
rename s4q4b covid_feb_remittances
rename s4q4c covid_feb_food_expenditures
rename s4q4d covid_feb_health_expenditures
rename s4q4e covid_feb_edu_expenditures
rename s4q4f covid_feb_transfers_abroad
rename s4q4g covid_feb_savings
rename s4q4h covid_feb_debt

rename s5q1 hh_n_above_18
rename s5q2 hh_n_children
rename s5q3 hh_n_bedrooms
rename s5q5 hh_revenue_range_r3
rename s5q6 hh_formal_savings
rename s5q7 hh_informal_savings
rename s5q8 hh_formal_debt
rename s5q9 hh_informal_debt


********************************************************************************
*  CLEANING AND RECODING
********************************************************************************

gen survey_round = 3

* -------------------- ID, treatment, sex -------------------- *

destring	id treat sex, replace

* -------------------- Consent -------------------- *

recode		consent (2=0)

label def	yesno 0 "No" 1 "Yes"
label val	consent yesno

* -------------------- Job Before Eiffage ------------------ *

recode 		bef_eif_job(2 = 0)
label value bef_eif_job yesno

* ------------------- Still Working at Eiffage --------------------- *

recode 		eif_still_work(2=0)
label value eif_still_work yesno

* -------------------- Position at Eiffage -------------------- *

replace 	eif_pos = 21	if 		s1aq5a_a == "Aide grutiers"
replace 	eif_pos = 22	if 		s1aq5a_a == "Assainissement"
replace 	eif_pos = 11	if 		s1aq5a_a == "OUVRIER" 
replace 	eif_pos = 23	if 		s1aq5a_a == "Chef d’équipe Technicien de surface"
replace 	eif_pos = 25 	if 		s1aq5a_a == "FOSSER"
replace 	eif_pos = 14	if 		s1aq5a_a == "Préventeur"
replace 	eif_pos = 17	if 		s1aq5a_a == "Étencite"
replace 	eif_pos = 19	if 		s1aq5a_a == "Responsable QHSE" | s1aq5a_a == "Superviseur QHSE"
replace 	eif_pos = 21	if 		s1aq5a_a == "Soudeur"
replace 	eif_pos = 27	if 		s1aq5a_a == "Terrassement"
replace 	eif_pos = 25	if 		s1aq5a_a == "MANGASINIER"
replace 	eif_pos = 26	if 		s1aq5a_a == "RÉSEAU MULTITIPULAIRE"

drop		s1aq5a_a

label def	eif_pos ///
    1 "Security agent helper"	2 "Guardian"	///
    3 "Mason"					4 "Scrap dealer"	///
    5 "Form setter"				6 "Carpenter"	///
    7 "Laborer"					8 "Other"	///
    9 "Driver help"				10 "Topographer/Topographer helper"	///
    11 "Worker"					12 "Flag holder"	///
    13 "Specialized laborer/worker"  ///
    14 "Preventive agent"		15 "Bile"	///
    16 "Painter"				17 "Waterproofing" ///
    18 "Restauration"			19 "QHSE supervisor/manager"  ///
    20 "Plaster worker"			21 "Welder"	///
    21 "Crane helper" 			22 "Sanitation" ///
    23 "Cleaner team leader" 	///
    24 "Fosseur"				25 "Workhouse worker" ///
    26 "Multi-pole Network Worker" 	27 "Earthwork worker" 
                
label val	eif_pos eif_pos

* ----------------  Activity Status in the last 7 days  ---------------- * 

label def 	last7d_status ///
    1 "Active, busy" ///
    2 "Student" ///
    3 "Unemployed" ///
    4 "Retired" ///
    5 "Housewife (female or male)" ///
    6 "Other inactive"


label value last7d_status last7d_status

* ------------------  Current Work Sector  -------------------- *

* construction
replace currwork_sector = 1 if s1bq6a_a == "Coffreur" | s1bq6a_a == "Superviseur de chantier" | s1bq6a_a == "Peintre" | s1bq6a_a == "Peinture"

* négociant/ventes/ vente de détails 
replace currwork_sector = 18 if s1bq6a_a == "Agent commercial" | s1bq6a_a == "Commerce" | s1bq6a_a == "GERANT MULTISERVICES" | s1bq6a_a == "Petit commerce" | s1bq6a_a == "VENDEUSE DE PRODUITS DIVERS" | s1bq6a_a == "Tailleur" 

replace currwork_sector = 30 if s1bq6a_a == "ELEVAGE" | s1bq6a_a == "Cultivateur" | s1bq6a_a == "Pêche"

* surveillant de parking payant into "Sécurité/Gardien/Soldat"
replace currwork_sector = 26 if s1bq6a_a == "Surveillant de parking payant" 

* replacing two remaining s1bq6a_a strings with new categories created
replace currwork_sector = 38 if s1bq6a_a == "JOURNALIER"
replace currwork_sector = 39 if s1bq6a_a == "Vulgarisation"

drop s1bq6a_a

label def 	currwork_sector ///
    1 "Construction" ///
    2 "Administration and management" ///
    3 "Administration/Clerk/Office" ///
    4 "Accountant/Accountant finance/Procurement" ///
    5 "Nurse/Healthcare/Medical profession" ///
    6 "Teacher/Tutor" ///
    7 "Lawyer" ///
    8 "Engineer/Architect" ///
    9 "Journalist" ///
    10 "Psychologist" ///
    11 "Banker" ///
    12 "Hotel work (waiter incl.)" ///
    13 "Factory" ///
    14 "Wood and metal work, carpentry, craftmanship" /// 
    15 "Mechanics" ///
    16 "Machine operator" ///
    17 "Businessman" ///
    18 "Dealer/Salesman/Retail sales" ///
    19 "Electrician" ///
    20 "Driver" ///
    21 "Statistician/Data collection" ///
    22 "Beauty business/Hairdresser/Salon" ///
    23 "Cleaner" ///
    24 "Transport/Taxi driver" ///
    25 "Cook/baker" ///
    26 "Security/Guardian/Soldier" ///
    27 "Entertainment/arts" ///
    28 "Church/Priesthood" ///
    29 "Plumber/gas" ///
    30 "Agriculture, farming, fisherman" ///
    31 "Tourism/Touristic guide" ///
    32 "Laboratory technician" ///
    33 "IT technician/Mobile technician" ///
    34 "Other occasional/daily job" ///
    35 "Secretary" ///
    36 "Broker" ///
    37 "Other" ///
    38 "Daily worker" ///
    39 "Tire mechanic*"

lab val currwork_sector currwork_sector

* ------------------ Average Monthly Salary  -------------------- *

mvdecode 	earn_avg_month, mv(-888 -999 .999 999 888)
replace		earn_avg_month = . if earn_avg_month>0 & earn_avg_month<1

replace earn_avg_month_range = 1 if earn_avg_month >= 0 & earn_avg_month < 80000
replace earn_avg_month_range = 2 if earn_avg_month >= 80000  & earn_avg_month < 130000
replace earn_avg_month_range = 3 if earn_avg_month >= 130000 & earn_avg_month < 180000
replace earn_avg_month_range = 4 if earn_avg_month >= 180000 & earn_avg_month < 230000
replace earn_avg_month_range = 5 if earn_avg_month >= 230000 & earn_avg_month < 280000
replace earn_avg_month_range = 6 if earn_avg_month >= 280000 & earn_avg_month != .


label def monthly_income_range ///
    1 "Less than 80,000 (CFAF)" ///
    2 "80,000–130,000" ///
    3 "130,000–180,000" ///
    4 "180,000–230,000" ///
    5 "230,000–280,000" ///
    6 "More than 280,000" ///

label value earn_avg_month_range 	monthly_income_range

mvdecode 	earn_last_month, mv(-888 -999 .999 999 888)
replace		earn_last_month = . if earn_last_month>0 & earn_last_month<1

recode 		earn_change_jan20(2 = 0)
label value earn_change_jan20 yesno

recode		earn_dir_change_jan20 (2 = -1)
label def   direction_change 		///
    1 "Increase"		///
    -1 "Decrease"
label value earn_dir_change_jan20 direction_change

label def salary_change_amount ///
    1 "Less than 5,000 CFAF" ///
    2 "5,001 - 10,000" ///
    3 "10,001 - 15,000" ///
    4 "15,001 - 20,000" ///
    5 "20,001 - 25,000" ///
    6 "25,001 - 30,000" ///
    7 "30,001 - 35,000" ///
    8 "35,001 - 40,000" ///
    9 "More than 40,000 CFAF"

label value earn_amount_change_jan20 salary_change_amount

mvdecode 	last7d_earn_avg, mv(-888 -999)

lab def		wage_freq ///
    1 "Hourly" ///
    2 "Daily" ///
    3 "Weekly" ///
    4 "Fortnightly" ///
    5 "Monthly" ///
    6 "Other"

lab val		last7d_earn_freq wage_freq

* hourly salary - recall s1bq14 reports hours worked in last 7 days
replace last7d_earn_avg = last7d_earn_avg*last7d_hours_worked if  last7d_earn_freq == 1 

* daily salary - recall s1bq13 specifies the number of days spent working in the last week
replace last7d_earn_avg = last7d_earn_avg*last7d_days_worked if last7d_earn_freq == 2 

* fortnightly salary: 
replace last7d_earn_avg = last7d_earn_avg/2 if last7d_earn_freq == 4

* monthly salary:
replace last7d_earn_avg = last7d_earn_avg/4 if last7d_earn_freq == 5

replace last7d_earn_avg = . if s1bq15e_a == "ne veut pas le dire"

replace last7d_earn_freq = 3 if s1bq15e_a == "Travail pour son propre compte"

replace last7d_earn_avg = . if s1bq15e_a == "PAS ENCORE RECU DE SALAIRE"

* procedure: divide s1bq15d by 2 to get daily wage and multiply by days of work
replace last7d_earn_avg = (last7d_earn_avg/2)*last7d_days_worked if s1bq15e_a == "Chaque deux jours"

* s1bq15e_a == "Par commande", no way of knowing frequeny of orders so missing 
replace last7d_earn_avg = . if s1bq15e_a == "Par commande"

/* 
    All salary amounts specified in last7d_earn_avg should now be 
    weekly salary so I change the salary frequency variable to 3 
    (weekly) for all non missing values 
*/
replace last7d_earn_freq = 3 if last7d_earn_avg != .

drop s1bq15e_a

mvdecode earn_*, mv(-888 -999)

* -------------------- Quit company reason -------------------- *

replace 	s1bq5a_a = "" 		if 		s1bq5a_a == "99" 

replace 	eif_quit_reason = 2 if 		s1bq5a_a == "Fin de contat" | s1bq5a_a == "Fin de contrat" | s1bq5a_a == "Fin de contrat. Ils ont signé avec un sous traitant pour continuer les travaux"

* also categorizing "fin de chantier" and "plus assez de travail" as "Fin de contrat: poste plus nécessaire au TER"
replace 	eif_quit_reason = 2 if 		s1bq5a_a == "FIN CHANTIER" | s1bq5a_a == "FIN DE CHANTIER" | s1bq5a_a == "Fin chantier" | s1bq5a_a == "Fin de chantier" | s1bq5a_a == "LE CHANTIER EST TERMINER ET ON NOUS A DIT DATTENDRE LA DEUXIEUME TROCHON QUI VA DEMARRER SOUS PEU DE TEMPS" | s1bq5a_a == "Les travaux du TER ont diminué" | s1bq5a_a ==  "Ya  plus assez de travail" | s1bq5a_a == "Ya plus assez de travail" | s1bq5a_a == "Ont les a diminuer parceque sa reste pas beaucoup de travail"

replace 	eif_quit_reason = 4 if 		s1bq5a_a == "Non renouvele" 

replace 	eif_quit_reason = 3 if 		s1bq5a_a == "Arrêt sans motif" | s1bq5a_a == "A reçu un préavis de licenciement"

* raison personelle/familiale
replace 	eif_quit_reason = 7 if 		s1bq5a_a == "Il avait un parent malade au village et c’est lui qui devait le soutenir"

drop 		s1bq5a_a

label define  eif_quit_reason							    ///
    1 "Found another job and quited"		            	///
    2 "End of contract: Position not necessary at TER"  	///
    3 "End of contract: Dismissal"							///	
    4 "End of contract: Contract not renewed"       	    ///		
    5 "Other reason"					 					///	
    6 "Partial unemployment because of Covid-19"		 	///	
    7 "Personal/family reasons" 					 		///
        
        
label value eif_quit_reason 	eif_quit_reason

* ------------------------ Covid-19 health --------------------------- *

lab values covid_protect_m* yesno

mvdecode covid_sympt_*, mv(-888 -999)
recode covid_sympt_* (2=0)
lab values covid_sympt_* yesno

* ------------------------ Covid-19 economics --------------------------- *

mvdecode covid_hhm_lost_job, mv(-888 -999)
recode covid_hhm_lost_job (2=0)
lab values covid_hhm_lost_job yesno

* ------------------- Household Information -------------------- *

mvdecode hh_*, mv(-888 -999)

label def 	hh_formal_savings /// 
    0 "Nothing (0 FCFA)" ///
    1 "Less than 50,000 (FCFA)" ///
    2 "50,000-100,000" ///
    3 "100,000-150,000" ///
    4 "150,000-200,000" ///
    5 "200,000-250,000" ///
    6 "250,000 and more"

label value hh_formal_* hh_informal_* hh_formal_savings

* ----------- Changing Work Conditions due to Covid-19 ------------- *		

* add value labels to binary variables
forval v = 1/5 {
    label 		define 	covid_change_work_cond`v' 0 "Non" 1 "Oui", replace
    label		values	covid_change_work_cond`v'  	covid_change_work_cond`v'
}
** Combination 2 + 3 is inconsistent— hours worked cannot decrease and increase at the same time
** Turn combinations 2+3 and 4+5 to missing for now:

replace 	covid_change_work_cond2 = . 	if 		covid_change_work_cond   == "2 3"
replace 	covid_change_work_cond3 = . 	if 		covid_change_work_cond   == "2 3"

replace 	covid_change_work_cond4 = . 	if 		covid_change_work_cond   == "4 5"
replace 	covid_change_work_cond5 = . 	if 		covid_change_work_cond   == "4 5"

drop covid_change_work_cond

********************************************************************************
*  ORDER VARIABLES AND SAVE
********************************************************************************

* Save harmonized baseline data
save "$data/2_processed/followup2_clean.dta", replace

********************************************************************************
*  END CODE
********************************************************************************
